﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace Assignment2
{
    public class OrderProcessing
    {
        //************************************************************************
        //*
        //*  Member variables
        //*
        //************************************************************************

        private Int32 _price;
        
        //************************************************************************
        //*
        //*  Constructor
        //*
        //************************************************************************

        public OrderProcessing( Int32 price)
        {
            _price = price;
        }

        //************************************************************************
        //*
        //*  Get/set the price
        //*
        //************************************************************************

        public Int32 Price
        {
            get { return _price; }
            set { _price = value; }
        }

        //************************************************************************
        //*
        //*  Add an order to be processed
        //*
        //************************************************************************

        public void add(Order order)
        {
            OrderProcessingThread processor =
                new OrderProcessingThread(_price, order);

            Thread order_thread = 
                new Thread(
                    new ThreadStart(processor.handle_order));

            order_thread.Start();
        }
    }

    //************************************************************************
    //*
    //*  Thread Class to process orders
    //*
    //************************************************************************

    public class OrderProcessingThread
    {
        //************************************************************************
        //*
        //*  Member variables
        //*
        //************************************************************************

        private Int32 _price;
        private double _taxRate;
        private Int32 _shipping;

        private Order _order;

        // retailer shutdown event
        public static event OrderProcessingEvent order_complete;
        
        //************************************************************************
        //*
        //*  Constructor
        //*
        //************************************************************************

        public OrderProcessingThread(
            Int32 price,
            Order order)
        {
            _price = price;
            _taxRate = 0.1;
            _shipping = 100;

            _order = order;

        }

        //************************************************************************
        //*
        //*  Process an order and return the results to the client
        //*
        //************************************************************************

        public void handle_order()
        {
            // check the card number for a valid card number
            if (validCard(_order.getCardNo()) == true)
            {
                // calculate the total
                Int64 sub_total = (_price * _order.getNumberOfChickens());
                Int64 tax = (Int64)((double)sub_total * _taxRate);
                Int64 total = sub_total + tax + _shipping;

                // complete the order
                _order.completeOrder();

                // set the price total
                _order.PriceTotal = total;
                _order.SubTotal = sub_total;

                // send confirmation
                //this._sendConfirmation( _client, _order, total);
                order_complete(_order);

                // log the order
                OrderReceiptLog.log_message(_order);
            }

        }

        //************************************************************************
        //*
        //*  Check for the validity of a card
        //*
        //************************************************************************

        private bool validCard(Int64 card_number)
        {
            // range of card numbers considered valid
            Int64 min_card_number = 0;
            Int64 max_card_number = 9999999999999999;

            if (card_number >= min_card_number &&
                card_number <= max_card_number)
            {
                return true;
            }

            // send error notification
            Logger.log_message("Error: invalid card number!\r\n");
            return false;
        }       
    }
}
